package com.tansun.rule.utils;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.alibaba.fastjson.JSON;
import com.tansun.rule.config.AppConfiguration;
import com.tansun.rule.exception.ConvertException;

public class RuleBomMapJsonConvertProject extends RuleBomMapJsonConvert {

	/**
	 * 按照规则项目转换
	 * @param ruleProject
	 * @param inputXml
	 * @return
	 * @throws ConvertException
	 */
	public Map<String, Object> convertBusinessMap(String ruleProject, String inputXml) throws ConvertException{
		try {
//			if(AppConfiguration.RuleProjMetaMap.get(ruleProject) == null){
//				//String path = AppConfiguration.APPHOMEDIR+AppConfiguration.RuleProjDeployDir+"/"+ruleProject+"/"+AppConfiguration.BomFile;
//				String path = AppConfiguration.RuleProjDeployDir+"/"+ruleProject+"/"+AppConfiguration.BomFile;
//				File filemap = new File(path);
//				Map<String, MClass> metaMap = RuleBomMapXmlConvert.getMapping(filemap);
//				//规则执行系统信息
//				metaMap.put("System", buildSystemMClass());
//				AppConfiguration.RuleProjMetaMap.put(ruleProject, metaMap);
//			}
			Map convertinsertMap = json2bom(inputXml,AppConfiguration.RuleProjMetaMap.get(ruleProject));
			return convertinsertMap;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConvertException("convert xml to bom error");
		}
	}
	/**
	 * 额外转换信息
	 * @return
	 */
	private static MClass buildSystemMClass(){
		MClass mclass = new MClass();
		mclass.setName("System");
		mclass.setParamType("OUT");
		mclass.setChzn("执行信息");
		
		MField mfield0 = new MField();
		mfield0.setName("entryPoint");
		mfield0.setType("String");
		MField mfield1 = new MField();
		mfield1.setName("invokeTimes");
		mfield1.setType("Long");
		MField mfield2 = new MField();
		mfield2.setName("isSuccess");
		mfield2.setType("String");
		MField mfield3 = new MField();
		mfield3.setName("exception");
		mfield3.setType("String");
		MField mfield4 = new MField();
		mfield4.setName("exceptiontType");
		mfield4.setType("String");
		MField mfield5 = new MField();
		mfield5.setName("id");
		mfield5.setType("String");
		
		mclass.getFieldMap().put("entryPoint", mfield0);
		mclass.getmFieldNames().add("entryPoint");
		mclass.getFieldMap().put("invokeTimes", mfield1);
		mclass.getmFieldNames().add("invokeTimes");
		mclass.getFieldMap().put("isSuccess", mfield2);
		mclass.getmFieldNames().add("isSuccess");
		mclass.getFieldMap().put("exception", mfield3);
		mclass.getmFieldNames().add("exception");
		mclass.getFieldMap().put("exceptiontType", mfield4);
		mclass.getmFieldNames().add("exceptiontType");
		mclass.getFieldMap().put("id", mfield5);
		mclass.getmFieldNames().add("id");
		return  mclass;
	}
	/**
	 * 按照规则项目转换
	 * 
	 * @param ruleProject
	 * @param outMap
	 * @return
	 */
	public String convertBusinessJson(String ruleProject, Map<String, Object> outMap){
		
//		if(AppConfiguration.RuleProjMetaMap.get(ruleProject) == null){
//			String path = AppConfiguration.APPHOMEDIR+AppConfiguration.RuleProjDeployDir+"/"+ruleProject+"/"+AppConfiguration.BomFile;
//			File filemap = new File(path);
//		}
		//规则执行系统信息
		if(AppConfiguration.RuleProjMetaMap.get(ruleProject).get("System")==null){
			Map<String, MClass> metaMap = AppConfiguration.RuleProjMetaMap.get(ruleProject);
			metaMap.put("System", buildSystemMClass());
			AppConfiguration.RuleProjMetaMap.put(ruleProject, metaMap);
			
		}
		
		String json = bom2Json(outMap, AppConfiguration.RuleProjMetaMap.get(ruleProject));
		return json;
	}
	
	
	public  String   convertBuzListJson(String ruleProject, List<Map<String, Object>> outMap) {
		if(AppConfiguration.RuleProjMetaMap.get(ruleProject).get("System")==null){
			Map<String, MClass> metaMap = AppConfiguration.RuleProjMetaMap.get(ruleProject);
			metaMap.put("System", buildSystemMClass());
			AppConfiguration.RuleProjMetaMap.put(ruleProject, metaMap);
			
		}
		return   JSON.toJSONString(outMap);
	}
	
	/**
	 * 衍生变量计算
	 * @param dataMap
	 * @param metaMap
	 */
	public static void doDerives(Map<String, Object> dataMap, String ruleProject){
		Map<String, MClass> metaMap = AppConfiguration.RuleProjMetaMap.get(ruleProject);
		HashMap<String, Object> mathMap = new HashMap<String, Object>();
		
		Set<String> ckeys = metaMap.keySet();
		for (String mclassname : ckeys) {
			Map<String, MField> mfMap = metaMap.get(mclassname).getFieldMap();
			
			Set<String> fkeys = mfMap.keySet();
			for (String mfieldname : fkeys) {
				Object subDataMap = dataMap.get(mclassname);
				if(subDataMap!=null){
					mathMap.put(mfMap.get(mfieldname).getId(), ((HashMap<String, Object>)subDataMap).get(mfieldname));
					
				}
			}
		}
		for (String mclassname : ckeys) {
			Map<String, MField> mfMap = metaMap.get(mclassname).getFieldMap();
			
			Set<String> fkeys = mfMap.keySet();
			for (String mfieldname : fkeys) {
				if("true".equalsIgnoreCase(mfMap.get(mfieldname).getIsDerives())){
					Object result = MathHelper.invokeMath(mathMap,  mfMap.get(mfieldname).getMathStr());
					((HashMap<String, Object>)dataMap.get(mclassname)).put(mfieldname, result);
				}
			}
		}
		
	}
}
